package com.zx.mobile.service.proxy;

import com.zx.map.service.domain.OrdLogDomain;
import com.zx.map.service.model.OrdLogModel;
import com.zx.map.utils.SUConstants;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.mobile.domain.OrdListDaily;
import com.zx.platform.utils.DateUtils;
import com.zx.platform.utils.PmpUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.util.*;

public class OrderQueryProxy {

    /**
     * 模型转换
     *
     * @param user
     * @param ordQueryDomain
     * @return
     */
    public static OrdLogDomain transformQueryDomain(MobileUserDomain user,
                                                    OrdLogDomain ordQueryDomain) {
        if (StringUtils.isBlank(ordQueryDomain.getBeginDate())) {
            ordQueryDomain.setBeginDate(PmpUtils.dateToString(
                    DateUtils.getMonthFirstDateOf(new Date()), "yyyy-MM-dd"));
        }
        if (StringUtils.isBlank(ordQueryDomain.getEndDate())) {
            ordQueryDomain.setEndDate(PmpUtils.dateToString(new Date(),
                    "yyyy-MM-dd"));
        }
        ordQueryDomain.setMerchantId(user.getMerchantInfoDetailModel()
                .getMerchantId());
        return ordQueryDomain;
    }

    /**
     * 汇总统计
     *
     * @param ordList
     * @return
     */
    public static Map<String, Object> transformOrdList(List<OrdLogModel> ordList) {
        Map<String, Object> map = new HashMap<String, Object>();
        List<OrdListDaily> list = new ArrayList<OrdListDaily>();
        Map<String, OrdListDaily> contain = new HashMap<String, OrdListDaily>();
        BigDecimal totalAmt = new BigDecimal("0");
        for (OrdLogModel temp : ordList) {
            StringBuffer tempAccount = new StringBuffer("");
            String date = PmpUtils.dateToString(temp.getCreateDate(),
                    "yyyy/MM/dd");
            totalAmt = totalAmt.add(new BigDecimal(temp.getPaymentAmt()
                    .toString()));
            //处理账号隐藏  姓名隐藏等内容
            if (SUConstants.TRANSTYPE_NOCARDINCOME.equals(temp.getTransType())) {
                for (int j = 0; j < temp.getBankAccount().length(); j++) {
                    if (j >= 4 && j < temp.getBankAccount().length() - 4) {
                        tempAccount.append("*");
                    } else {
                        tempAccount.append(temp.getBankAccount().charAt(j));
                    }
                }
                temp.setShortCardNo(tempAccount.toString());
            }
            if (contain.containsKey(date)) {
                // map中包含数据的 增加数据
                OrdListDaily ordDaily = contain.get(date);
                ordDaily.getOrdLogList().add(temp);
            } else {
                // map中不包含数据的 构造数据
                OrdListDaily ordListDaily = new OrdListDaily();
                List<OrdLogModel> dateList = new ArrayList<OrdLogModel>();
                ordListDaily.setOrdDate(date);
                dateList.add(temp);
                ordListDaily.setOrdLogList(dateList);
                list.add(ordListDaily);
                contain.put(date, ordListDaily);
            }
        }
        Collections.sort(list, new Comparator<OrdListDaily>() {
            public int compare(OrdListDaily o1, OrdListDaily o2) {
                return o2.getOrdDate().compareTo(o1.getOrdDate());
            }
        });
        for (OrdListDaily ordTemp : list) {
            Collections.sort(ordTemp.getOrdLogList(),
                    new Comparator<OrdLogModel>() {
                        public int compare(OrdLogModel o1, OrdLogModel o2) {
                            return o2.getCreateDate().compareTo(
                                    o1.getCreateDate());
                        }
                    });
        }
        map.put("list", list);
        map.put("totalAmt", totalAmt.doubleValue());
        return map;
    }

}
